<?php


class WebSocketServer
{
    public $ws;
    static $ssl = true;
    private $connectConf = array('ip'=>'','port'=>8888);

    public function __construct($conf = array())
    {
        $conf = array_merge($this->connectConf,$conf);
        $tcp = SWOOLE_SOCK_TCP;
        if ($this::$ssl){
            $tcp = SWOOLE_SOCK_TCP | SWOOLE_SSL;
        }
        $this->ws = new swoole_websocket_server($conf['ip'],$conf['port'],SWOOLE_PROCESS,$tcp);
        $this->ws->table = $this->setTable();
        $this->ws->redis = $this->setRedis();
        $setConf = array(
            'task_worker_num' => 2,
            'worker_num'      => 2,
            'daemonize'       => 1,
            'heartbeat_check_interval' => 300,
            'heartbeat_idle_time'      => 600
        );
        if ($this::$ssl){
            $setConf['ssl_cert_file'] = '/usr/local/ssl/Nginx/*.crt';
            $setConf['ssl_key_file'] = '/usr/local/ssl/Nginx/*.key';
        }
        $this->ws->set($setConf);
        $this->ws->on('start',[$this,'onStart']);
        $this->ws->on('open',[$this,'onOpne']);
        $this->ws->on('message',[$this,'onMessage']);
        $this->ws->on('task',[$this,'onFinish']);
        $this->ws->on('close',[$this,'onClose']);

        $this->ws->start();
    }
}